<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.classname"  placeholder="班级名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getClasses">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleEdit">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="classes" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="classname" label="班级名称" width="120"></el-table-column>
			<el-table-column prop="headmaster" label="班主任"></el-table-column>
			<el-table-column prop="subject" label="学科"></el-table-column>
			<el-table-column prop="classnum" label="班级编号"></el-table-column>

			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="班级名称" prop="classname">
					<el-input v-model="editForm.classname" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="班级编号" prop="classnum">
					<el-input v-model="editForm.classnum" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="学科" prop="subject">
                    <el-select v-model="editForm.subject" filterable size="70px" placeholder="请选择" @change="handleSubjectChange($event)"  style="width:200px;">
                        <el-option v-for="item in subjectoptions" :key="item.id" :label="item.subjectname" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="班主任" prop="headmaster">
					<el-select v-model="editForm.headmaster" filterable size="70px" placeholder="请选择" style="width:200px;">
                        <el-option v-for="item in teacheroptions" :key="item.id" :label="item.name" :value="item.name"></el-option>
                    </el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--答辩分配界面-->
		<el-dialog title="答辩分配" v-model="distributionFormVisible" :close-on-click-modal="false">
			<el-form :model="distributionForm" label-width="80px" :rules="distributionFormRules" ref="distributionForm">
				<el-form-item label="答辩编号" prop="defenceNo" >
					<el-select v-model="distributionForm.defenceNo" size="70px" placeholder="请选择">
						<el-option
								v-for="item in options"
								:key="item.value"
								:label="item.value"
								:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<div style="text-align: center" >
					<el-transfer
							style="text-align: left; display: inline-block"
							v-model="distributionForm.distributionValue"
							filterable
							:left-default-checked="[]"
							:right-default-checked="[]"
							:titles="['班级信息', '考核班级']"
							:button-texts="['移除', '添加']"
							:format="{
							noChecked: '${total}',
							hasChecked: '${checked}/${total}'
						  }"
							@change="handleChange"
							:data="aacc">
						<span slot-scope="{ option }">{{ option.key }} - {{ option.label }}</span>
						<!--<el-button class="transfer-footer" slot="left-footer" size="small">操作</el-button>
						<el-button class="transfer-footer" slot="right-footer" size="small">操作</el-button>-->
					</el-transfer>
				</div>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="distributionFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="submitDistribution" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getClassListPage, removeClass, batchRemoveClass, editClass } from '../../api/api';

	export default {
        data() {
            return {
                distributionValue: [1],
                options:[],
                subjectoptions:[],
                teacheroptions:[],
                aacc: [],
                filters: {
                    value: '',
                    type: '',
                    typeName: ''
                },
                classes: [],
                total: 0,
                currentPage: 1,
                pageSize: 10,
                listLoading: false,
                subjectid:0,
                subjectname:'',
                sels: [],//列表选中列
                selectList: [],//选中答辩班级的list,
				selectNo:'',
                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                addLoading:false,
                editFormRules: {
                    classname: [
                        {required: true, message: '请输入班级名称', trigger: 'blur'}
                    ],
                    classnum: [
                        {required: true, message: '请输入班级编号', trigger: 'blur'}
                    ],
                    subject: [
                        {required: true, message: '请输入学科', trigger: 'blur'}
                    ]
                },
                //编辑界面数据
                editForm: {
                    value: '',
                    type: '',
                    typeName: ''
                },

                distributionFormVisible: false,//答辩分配界面是否显示
                // addLoading: false,
                distributionFormRules: {

                    value: [
                        {required: true, message: '请选择答辩编号', trigger: 'blur'}
                    ]
                },
                //新增界面数据
                distributionForm: {
                    defenceNo: '',
                    distributionValue: []

                }


            }
        },
        methods: {
            //获取答辩信息列表
			getClassList(){
			    let para = {type : 2};
                this.$http.post("/class/classList", para).then((res) => {
                    this.options = res.data;
                });
                //查询班级穿梭框信息
                let para1 = {type : 1};
                this.$http.post("/class/classList", para1).then((res) => {
                    let data = res.data;
                    const dataList = [];
                    for (let i = 0; i <data.length; i++) {
                        let key = data[i].value;
                        dataList.push({
                            key: key,
                            label: key
                            //disabled: i % 4 === 0
                        });
                    }
                    this.aacc = dataList;
                });
			},
			// 获取学科列表
			getSubjectList(){
                this.$http.post("/subject/list").then((res) => {
                    this.subjectoptions = res.data;
                });
			},
			getTeacherList(){
			    this.$http.post("/teacher/listall").then((res) => {
                    this.teacheroptions = res.data;
                });
			},
            handleChange(value, direction, movedKeys) {
              this.selectList = value;
            },
            handleSubjectChange(value){
                let obj = {};
				obj = this.subjectoptions.find((item)=>{
					return item.id === value;//筛选出匹配数据
				});
				this.subjectid = obj.id;
				this.subjectname = obj.subjectname;
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.getClasses();
            },
            //获取用户列表
            getClasses() {
                let para = {
                    currentPage: this.currentPage,
                    classname: this.filters.classname
                };
                this.listLoading = true;
                this.$http.post("/class/listpage", para).then((res) => {
                    this.total = res.data.total;
                    this.classes = res.data.list;
                    this.listLoading = false;
                });
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    removeClass(row.id).then((res) => {
                        this.listLoading = false;
                        if(res.data == ""){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getClasses();
                        }else{
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                        }
                    });
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
            },

            //显示答辩分配界面
            distribution: function () {
                this.distributionFormVisible = true;
                this.distributionForm = {
                     distributionValue: [],
                    defenceNo: ''
                };
            },
			//答辩分配信息入库
			submitDistribution:function(){
                let param = {
                      selectNo : this.distributionForm.defenceNo,
                     selectList : this.selectList
				}
                this.$http.put("/class/distribution", param).then((res) => {

				});
			},
            //编辑
            editSubmit: function () {

                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            let para = Object.assign({}, this.editForm);
                            para.subjectid = this.subjectid;
                            para.subject = this.subjectname;
                            editClass(para).then((res) => {
                                this.editLoading = false;
                                if(res.data == ""){
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    this.$refs['editForm'].resetFields();
                                    this.editFormVisible = false;
                                    this.getClasses();
                                }else{
                                    this.$message({
                                        message: res.data,
                                        type: 'error'
                                    });
                                }
                            });
                        });
                    }
                });
            },

            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id).toString();
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    batchRemoveClass(ids).then((res) => {
                        this.listLoading = false;
                        if(res.data == ""){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getClasses();
                        }else{
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                        }
                    });
                }).catch(() => {

                });
            }
        },
        mounted() {
            this.getClasses();
            this.getClassList();
            this.getSubjectList();
            this.getTeacherList();
        }
    }

</script>

<style scoped>

</style>